package com.zhf.dao.impl;

import com.zhf.dao.ClassDao;
import com.zhf.entity.Class1;
import com.zhf.utils.ConnectionUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements ClassDao {
    @Override
    public List<Class1> queryAllClass(int begin, int size) {
        String sql = "select class_no,class_name,grade,header_teacher,class_slogan,class_number from class limit ?,?";
        List<Class1> class1List = fromResultGetList(begin, size, sql);
        return class1List;
    }

    @Override
    public int queryCount() {
        int result = 0;
        List<Integer> list = new ArrayList<>();
        String sql = "select count(1) from class";
        ConnectionUtil.DQL(sql, new Object[]{}, (rs -> {
            try{
                if(rs.next()){
                    int res =rs.getInt(1);
                    list.add(res);
                }
            }
            catch (SQLException e){
                e.printStackTrace();
            }
        }));
        if(list.size() == 1){
            result =list.get(0);
        }
        return result;
    }

    @Override
    public Class1 queryClassByClassNo(String classNo) {
        String sql = "select class_no,class_name,grade,header_teacher,class_slogan,class_number from class where class_no = ?";
        List<Class1> class1List = new ArrayList<>();
        ConnectionUtil.DQL(sql,new Object[]{classNo}, rs -> {
            try{
                if(rs.next()){
                    Class1 class1 = new Class1();
                    tableToPojo(class1, rs, new Integer[]{1, 2, 3, 4, 5, 6});
                    class1List.add(class1);
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        });
        if(class1List != null && class1List.size() > 0){
            return class1List.get(0);
        }
        return null;
    }

    @Override
    public boolean insertClass1(Class1 class1) {
        String sql = "insert into class(class_no,class_name,grade,header_teacher,class_slogan,class_number) values(?, ?, ?, ?, ?, ?)";
        int rowId = -1;
        rowId = ConnectionUtil.DML(sql, new Object[]{class1.getClassNo(), class1.getClassName1(), class1.getGrade(), class1.getHeaderTeacher(), class1.getClassSlogan(), class1.getClassNumber()});
        if(rowId > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateClass(Class1 class1) {
        String sql = "update class set class_name = ?, grade = ?, header_teacher = ?, class_slogan = ?, class_number = ?  where class_no = ?";
        int rowId = -1;
        rowId = ConnectionUtil.DML(sql, new Object[]{class1.getClassName1(), class1.getGrade(), class1.getHeaderTeacher(), class1.getClassSlogan(), class1.getClassNumber(), class1.getClassNo()});
        if(rowId > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<Class1> queryClassByClassNameAndGrade(String className, String grade) {
        String sql = "select class_no,class_name,grade,header_teacher,class_slogan,class_number from class where class_name like ? and grade like ?";
        List<Class1> class1s = new ArrayList<>();
        ConnectionUtil.DQL(sql, new Object[]{className, grade}, rs -> {
            try{
                while (rs.next()){
                    Class1 class1 = new Class1();
                    tableToPojo(class1, rs, new Integer[]{1,2,3,4,5,6});
                    class1s.add((class1));
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        });
        return class1s;
    }

    @Override
    public List<Class1> queryClassByClassNos(String[] classNos) {
        String sql = "select class_no,class_name,grade,header_teacher,class_slogan,class_number from class where class_no in (";
        StringBuffer sb = new StringBuffer();
        if(classNos != null && classNos.length > 0){
            for(String  classNo : classNos){
                sb.append("?,");
            }
        }
        if(sb.length() > 1){
            sql = sql + sb.substring(0, sb.length() - 1);
        }
        sql = sql + ")";

        System.out.println(sql);
        List<Class1> class1List = new ArrayList<>();
        ConnectionUtil.DQL(sql, classNos, rs -> {
            try{
                while (rs.next()){
                    Class1 class1 = new Class1();
                    tableToPojo(class1, rs, new Integer[]{1,2,3,4,5,6});
                    class1List.add(class1);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        return class1List;
    }

    @Override
    public int deleteClassByClassNos(String[] classNos) {
        String sql = "delete from class where class_no in (";
        StringBuffer sb = new StringBuffer();
        if(classNos != null && classNos.length > 0){
            for (String studentId : classNos){
                sb.append("?,");
            }
        }
        if(sb.length() > 1){
            sql = sql + sb.substring(0, sb.length() - 1);
        }
        sql = sql + ")";
        int rowId = -1;
        rowId = ConnectionUtil.DML(sql, classNos);
        return rowId;
    }


    private List<Class1> fromResultGetList(Object obj1, Object obj2, String sql){
        List<Class1> class1List =new ArrayList<>();
        ConnectionUtil.DQL(sql, new Object[]{obj1, obj2}, rs -> {
            try{
                while (rs.next()){
                    Class1 class1 = new Class1();
                    tableToPojo(class1, rs, new Integer[]{1,2,3,4,5,6});
                    class1List.add(class1);
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        });
        return class1List;
    }

    public static void tableToPojo(Class1 class1, ResultSet rs, Integer[] resultIndex) throws SQLException{
        String classNo = rs.getString(resultIndex[0]);
        String className = rs.getString(resultIndex[1]);
        String grade = rs.getString(resultIndex[2]);
        String headerTeacher = rs.getString(resultIndex[3]);
        String classSlogan = rs.getString(resultIndex[4]);
        int classNumber = rs.getInt(resultIndex[5]);
        class1.setClassNo(classNo);
        class1.setClassName1(className);
        class1.setGrade(grade);
        class1.setHeaderTeacher(headerTeacher);
        class1.setClassSlogan(classSlogan);
        class1.setClassNumber(classNumber);
    }
}
